﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;
using Petroleum.Model;
using Petroleum.DB.Model;
using HZH_Controls.Forms;

namespace Petroleum.UC.Public
{
    /// <summary>
    /// Step 带删除添加行的表格控件
    /// </summary>
    public partial class GridToolTable : FrmWithTitle
    {

        public delegate void GetElistDelegate(List<Elist> eli);
        public GetElistDelegate ElistDelegate;
        List<Elist> elists = new List<Elist>();
        Elist elist = new Elist();
        public GridToolTable(string title, GetElistDelegate getElist, List<Elist> elists)
        {
            InitializeComponent();
            ElistDelegate = getElist;
            this.Title = title;
            this.IsShowCloseBtn = true;
            if (elists != null)
                this.elists = elists;
            InitiaDgv();
        }
        /// <summary>
        /// 初始化表格
        /// </summary>
        /// <param name="ColumnList">列集合</param>
        public void InitiaDgv()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Ename", HeadText = "Ename", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Etvd", HeadText = "Etvd", Width = 200, WidthType = SizeType.Absolute });
            dgv.RowHeight = 50;
            this.dgv.Columns = lstCulumns;
            this.dgv.DataSource = elists;
        }
        //private void Txtsstvd_TextChanged(object sender, EventArgs e)
        //{
        //    if (!string.IsNullOrWhiteSpace(txtsstvd.InputText))
        //    {
        //        lbltvd.Text = (0 - decimal.Parse(txtsstvd.InputText) + Session.BasicInf.KB) + "";
        //    }
        //}
        private void btnAdd_BtnClick(object sender, EventArgs e)
        {
            Elist eli = new Elist()
            {
                Ename = txtname.InputText,
                Etvd = txtsstvd.InputText
            };
            if (elist != null && !string.IsNullOrEmpty(elist.Ename))
            {
                elists.Remove(elist);
            }
            elists.Add(eli);
            this.dgv.DataSource = elists;
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                elists.RemoveAt(row);
                this.dgv.DataSource = elists;
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            elist = elists[this.dgv.SelectRow.RowIndex];
            if (elist != null)
            {
                txtsstvd.InputText = elist.Etvd + "";
                txtname.InputText = elist.Ename + "";
            }
        }
        private void btnOK_BtnClick(object sender, EventArgs e)
        {
            ElistDelegate(elists);
            this.Close();
        }
    }
}
